package com.luck.easyfoodbatch.ui;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.luck.easyfoodbatch.App;
import com.luck.easyfoodbatch.R;
import com.luck.easyfoodbatch.dialog.CustomDialog;
import com.luck.easyfoodbatch.dialog.ShowProgressDialog;
import com.luck.easyfoodbatch.mvp.mine.ForgetModel;
import com.luck.easyfoodbatch.mvp.mine.ForgetPresenter;
import com.luck.easyfoodbatch.mvp.mine.ForgetView;
import com.luck.easyfoodbatch.mvp.mine.SendVerificationModel;
import com.luck.easyfoodbatch.mvp.mine.SendVerificationPresenter;
import com.luck.easyfoodbatch.mvp.mine.SendVerificationView;
import com.luck.easyfoodbatch.mvp.model.ForgetPwdModel;
import com.luck.easyfoodbatch.mvp.model.SendModel;
import com.luck.easyfoodbatch.util.HttpUtil;
import com.luck.easyfoodbatch.util.T;
import com.luck.easyfoodbatch.util.Tools;

import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ForgetPasswordActivity extends BaseActivity implements ForgetView, SendVerificationView {

    @BindView(R.id.right_title)
    TextView rightTitle;
    @BindView(R.id.et_tel)
    EditText etTel;
    @BindView(R.id.ll_del)
    LinearLayout llDel;
    @BindView(R.id.et_code)
    EditText etCode;
    @BindView(R.id.btn_sendCode)
    Button btnSendCode;
    @BindView(R.id.et_psw)
    EditText etPsw;
    @BindView(R.id.et_psw_again)
    EditText etPswAgain;
    @BindView(R.id.btn_register)
    Button btnRegister;
    private Timer tm;
    private TimerTask tk;
    private int count = 60;
    private ForgetModel forgetModel;
    private SendVerificationModel verificationModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget_password, "忘记密码", "客服", MODE_BACK_NAVIGATION);
        ButterKnife.bind(this);
        etTel.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (editable.length() > 0) {
                    llDel.setVisibility(View.VISIBLE);
                } else {
                    llDel.setVisibility(View.GONE);
                }
            }
        });
    }

    @OnClick({R.id.right_title, R.id.ll_del, R.id.btn_sendCode, R.id.btn_register})
    public void onClick(View view) {
        String phone = etTel.getText().toString().trim();
        String psw = etPsw.getText().toString().trim();
        String code = etCode.getText().toString().trim();
        String pswAgain = etPswAgain.getText().toString().trim();
        switch (view.getId()) {
            case R.id.right_title:
                showPhoneDialog();
                break;
            case R.id.ll_del:
                etTel.setText("");
                break;
            case R.id.btn_sendCode:
                if (Tools.isNull(phone)) {
                    T.showToast(mContext, "请输入您的手机号");
                } else if (!Tools.validatePhone(phone)) {
                    T.showToast(mContext, "请输入正确手机号");
                } else {
                    showProgressDialog("正在获取，请稍候……");
                    verificationModel = new SendVerificationPresenter(this, mContext);
                    verificationModel.sendSms(phone);
                    startCount();
                }
                break;
            case R.id.btn_register:
                if (Tools.isNull(phone)) {
                    T.showToast(mContext, "请输入您的手机号");
                } else if (!Tools.validatePhone(phone)) {
                    T.showToast(mContext, "请输入正确手机号");
                } else if (Tools.isNull(code)) {
                    T.showToast(mContext, "请输入验证码");
                } else if (Tools.isNull(psw)) {
                    T.showToast(mContext, "请输入您的密码");
                } else if (psw.length() < 6) {
                    T.showToast(mContext, "密码长度不能小于6位");
                } else if (Tools.isNull(pswAgain)) {
                    T.showToast(mContext, "请再次输入密码");
                } else if (!psw.equals(pswAgain)) {
                    T.showToast(mContext, "再次密码输入不一致");
                } else {
                    showProgressDialog("正在修改，请稍候……");
                    forgetModel = new ForgetPresenter(this, mContext);
                    forgetModel.forgetPassword(phone, code, psw);
                }
                break;
        }
    }

    /**
     * 拨打电话
     */
    private void showPhoneDialog() {
        final CustomDialog dialog = new CustomDialog(mContext, App.screenWidth * 3 / 4,
                App.screenHeight / 4, R.layout.wind_base_dialog_xml, R.style.Theme_dialog);
        Button btn_cancel = (Button) dialog.findViewById(R.id.btn_cancel);
        Button btn_commit = (Button) dialog.findViewById(R.id.btn_commit);
        TextView tv_title = (TextView) dialog.findViewById(R.id.tv_title);
        TextView tv_content = (TextView) dialog.findViewById(R.id.tv_content);
        tv_title.setText("联系客服");
        btn_commit.setText("呼叫");
        tv_content.setText(HttpUtil.CALL_PHONE);
        btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        btn_commit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Tools.callPhone(HttpUtil.CALL_PHONE, mContext);
                dialog.dismiss();
            }
        });
        dialog.show();
    }

    /**
     * 启动时间器读秒
     */
    private void startCount() {
        tm = new Timer();
        tk = new TimerTask() {

            @Override
            public void run() {
                Message msg = mHandler.obtainMessage();
                count--;
                if (count > 0) {
                    msg.arg1 = count;
                    msg.what = 0;
                    msg.obj = count;
                    mHandler.sendMessage(msg);
                } else {
                    // 时间读完 初始秒数
                    mHandler.sendEmptyMessage(11);
                    count = 60;
                }

            }
        };
        tm.schedule(tk, 0, 1000);
    }

    private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    btnSendCode.setEnabled(false);
                    btnSendCode.setBackgroundResource(R.drawable.shape_round_solid_false_bg);
                    btnSendCode.setText(msg.arg1 + "秒");
                    btnSendCode.setTextColor(getResources().getColor(R.color.tab_color_false));
                    break;
                case 11:
                    btnSendCode.setEnabled(true);
                    btnSendCode.setBackgroundResource(R.drawable.shape_round_solid_bg);
                    btnSendCode.setText("发送验证码");
                    btnSendCode.setTextColor(getResources().getColor(R.color.white));
                    tk.cancel();
                    break;
            }
        }

    };

    @Override
    public void forgetSuccess(ForgetPwdModel pwdModel) {
        if (pwdModel.getStatus() == HttpUtil.RESULT_OK) {
            T.showToast(mContext, pwdModel.getMsg());
            finish();
        } else {
            T.showToast(mContext, pwdModel.getMsg());
        }
    }

    @Override
    public void httpStatusError() {
        T.showToast(mContext, getResources().getString(R.string.app_http_error));
    }


    @Override
    public void appSendSmsSuccess(SendModel sendModel) {
        if (sendModel.getStatus() == HttpUtil.RESULT_OK) {
            T.showToast(mContext, sendModel.getMsg());
        } else {
            T.showToast(mContext, sendModel.getMsg());
        }
    }

    @Override
    public void hideLoading() {
        hideShowProgress();
    }
}
